#ifndef DEF_MAINWINDOW
#define DEF_MAINWINDOW

#include <QMainWindow>

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QtGui>
#include "Cards.h"

#include "State.h"
#include "Player.h"
#include "Network.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

    public:
        MainWindow(size_t nbrPlayer, QString server = "", quint32 port = 57442);
        ~MainWindow();

        int whereIsPoint(const QPoint& point) const;

    protected:
       void closeEvent (QCloseEvent* event);
       void mousePressEvent(QMouseEvent* event);

    private slots:
        void phaseChanged(PHASE phase, quint32 playerID);
        void playerDefeated(quint32 ID);
        void refresh();
        void selectedPlayerChanged(int);
        void nextPhase();
        //void nextPlayer();
        void sendReinforcement();
        void displayInfos(size_t p);
        void reinStateIndexChanged();
        void attackingStateChanged(int index);
        void moveStateIndexChanged(int index);
        void battle();
        void move();
        void connected();
        void diceRoll(bool defensor);

        void localServerDown(int exitCode, QProcess::ExitStatus exitStatus);

    private:

        QList<int> adjacentsStates(const QString& from, const QList<int>& exclude = QList<int>());
        QStringList adjacentsStatesName(const QString& from);

        int findStateIndex(const QString& name);
        int findPlayerIndex(const QString& name);

        size_t rollTheDice(const QPoint& p);

        size_t m_nbrPlayer;
        QList<Player*> m_players;

        quint32 m_currPlay;
        PHASE m_currPhase;

        bool m_fighting;

        size_t m_nbrRound;

        bool m_first;


        QGraphicsView* m_graphicsView;
        QGraphicsScene* m_graphicsScene;

        QList<State> m_states;

        QComboBox* m_playerList;
        QLabel* m_playerListColor;
        QLabel* m_currentPlayer;
        QLabel* m_currentPlayerColor;

        QGroupBox* m_reinGB;
        QLabel* m_reinLeft;
        QComboBox* m_reinStateList;
        QSpinBox* m_reinSelect;
        QPushButton* m_reinGo;

        QGroupBox* m_atkGB;
        QComboBox* m_atkStateList;
        QComboBox* m_atkablStateList;
        QCheckBox* m_atkAuto;
        QPushButton* m_atkGo;

        bool m_mvmSwitch;
        QGroupBox* m_mvmGB;
        QComboBox* m_mvmStateList;
        QComboBox* m_mvmablStateList;
        QSpinBox* m_mvmSelect;
        QPushButton* m_mvmGo;

        QPushButton* m_nextPhase;

        QTextEdit* m_infos;

        QGroupBox* m_diceGB;
        QPushButton* m_defDice;
        QPushButton* m_attDice;
        QGraphicsView* m_diceView;
        QGraphicsScene* m_diceScene;
        QTextEdit* m_diceInfos;

        Cards* m_cards;

        Network* m_net;

        QProcess *m_server;

        bool m_local;
};

#endif
